package model

import (
	"errors"

	"git.oschina.net/ant180/scan/config"
	"github.com/xlvector/dlog"
)

type Model struct {
	Conf *config.ModelConf

	Mongodb *Mongodb
	Redis   *Redis
	SSDB    *SSDB
}

func NewModel(conf *config.ModelConf) (*Model, error) {
	if conf == nil {
		return nil, errors.New("conf is nil!")
	}
	m := &Model{
		Conf:    conf,
		Mongodb: nil,
		Redis:   nil,
		SSDB:    nil,
	}

	var err error
	if len(conf.Mongodb.Addr) != 0 {
		m.Mongodb, err = NewMongodb(&conf.Mongodb)
		if err != nil {
			dlog.Error("new mongodb fail! %v", err)
			return nil, err
		}
		dlog.Info("model mongodb was created!")
	}

	if len(conf.Redis.Addr) != 0 {
		m.Redis, err = NewRedis(&conf.Redis)
		if err != nil {
			dlog.Error("new redis fail! %v", err)
			return nil, err
		}
		dlog.Info("model redis was created!")
	}

	if len(conf.SSDB.Host) != 0 {
		m.SSDB, err = NewSSDB(&conf.SSDB)
		if err != nil {
			dlog.Error("new ssdb fail! %v", err)
			return nil, err
		}
		dlog.Info("model ssdb was created!")
	}

	return m, nil
}

//Notice
func (p *Model) Stop() {
}
